/*
 * axi_lite.cpp
 *
 *  Created on: May 30, 2023
 *      Author: lzc
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/mman.h>
#include "axi_lite.h"


#define AXI_LITE_SIZE 65535

int m_AxiLite_fd = -1;
unsigned int *m_AxiLite_MapBase = NULL;



int AXI_Lite_Init()
{
	m_AxiLite_fd = open("/dev/mem", O_RDWR | O_SYNC);
	if(m_AxiLite_fd < 0)
	{
		printf("open m_AxiLite_fd is fail\r\n");
		return -1;
	}

	m_AxiLite_MapBase = (unsigned int*)mmap(NULL, AXI_LITE_SIZE, PROT_READ | PROT_WRITE,MAP_SHARED, m_AxiLite_fd, 0x43C00000);
	if(m_AxiLite_MapBase == 0)
	{
		 printf("m_AxiLite_MapBase NULL pointer!\r\n");
		 return -1;

	}

	return 0;
}


unsigned int AXI_Lite_Read(int addr)
{
    unsigned int content;

    content = m_AxiLite_MapBase[addr];

	return content;
}


int AXI_Lite_Write(int addr,int value)
{
	unsigned int content;

	content = value;
	m_AxiLite_MapBase[addr] = content;


	return 0;
}


int AXI_Lite_Close()
{

	close(m_AxiLite_fd);
	munmap(m_AxiLite_MapBase, AXI_LITE_SIZE);

	return 0;
}

